package com.cms.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import com.action.DefaultAction;
import com.cms.model.Message;
import com.cms.service.MessageService;
import com.cms.service.MessageServiceImpl;

public class DownloadAction extends DefaultAction {

	public String dl()
	{
		try {
			HttpServletResponse response = super.getResponse();
			Message m = service.getById(f);
			InputStream is = new BufferedInputStream(new FileInputStream(m.getFilePath()+m.getRfileName()));
			byte[] b = new byte[is.available()];
			is.read(b);
			is.close();
			
			String fileName = m.getFileName();
	    	response.setContentType("application/octet-stream");
	    	response.setHeader("Location", fileName);
	    	response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
	    	OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
	    	outputStream.write(b);
	    	outputStream.flush();
	    	outputStream.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	String f = "";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1940882605107147866L;
	private MessageService service = new MessageServiceImpl();
	public String getF() {
		return f;
	}
	public void setF(String f) {
		this.f = f;
	}
	
}
